#!/bin/bash
#================================================
#check_diag script        	        	|
#                                               |
#                                               |
# Version 1.1					|
#	Changelog:				|
#		restarting down services	|
#		python gst launch support      	|
#						|
#Written by: Dan Merboth dcm6293@rit.edu        |
# 						|
#Usage:						|
# check_diag.sh (arg)                         	|
#================================================

#Displays help stuff.
function printHelp {
        echo ""
        echo "!----ONLY SPECIFY ONE----!"
	echo ""
        echo " Command | Expanded | Description"
        echo "---------|----------|--------------------------"
        echo " -v      | --vic    | Check for vic."
        echo "---------|----------|--------------------------"
        echo " -r      | --rat    | Check for rat."
        echo "---------|----------|--------------------------"
        echo " -g      | --grav   | Check for grav."
        echo "---------|----------|--------------------------"
        echo " -s      | --gst    | Check for gstreamer."
        echo "---------|----------|--------------------------"
        echo " -h      | --help   | Displays this help topic."
        echo "---------|----------|--------------------------"
        echo ""
        echo "!----ONLY SPECIFY ONE----!"
        echo ""
	echo "Useage: check_diag.sh [arg]"
	echo ""
} #printhelp

function checker {

    if [[ $Program == "vic" ]]; then

        #This works for whether vic has a space after it or a hyphen.

        PID=`ps aux | grep "vic[[:blank:][:punct:]]" | grep -v grep | awk '{print $2}'`

    elif [[ $Program == "rat" ]]; then

        PID=`pgrep rat-4.4.01`

    elif [[ $Program == "grav" ]]; then

        PID=`pgrep grav`

    elif [[ $Program == "gst" ]]; then

        PID=`pgrep gst-launch-`
        if [[ $PID = "" ]]; then
            #changed to search full process string for gst text
            PID=`pgrep -f gst-server-dv1394-\(usb-\)?h264-rtp.py`
        fi

    fi

    if [[ $PID == "" ]]; then
	echo "CRITICAL: $Program is not running"
        exit 2
    else
        echo "OK: $Program is running"
        exit 0

    fi

} #checker

#===============+
# Main script	|
#===============+

while [ $# -gt 0 ]
do

	case $1
        in
        -v) #VIC
        
            Program="vic"
        
        ;;
        --vic) #VIC long
        
            Program="vic"
        
        ;;
        -r) #RAT
        
            Program="rat"
        
        ;;
        --rat) #RAT long
        
            Program="rat"
        
        ;;
        -g) #Grav
        
            Program="grav"
        
        ;;
        --grav) #Grav long
        
            Program="grav"
        
        ;;
        -s) #Gstreamer
        
            Program="gst"
        
        ;;
        --gst) #Gstreamer long
        
            Program="gst"
        
        ;;
        -h) #help
        
            printHelp
            
        ;;
        --help)
        
            printHelp
            
        ;;
	esac
        shift 1
done
checker
exit
